package com.wutao.panzh.index.content.controller.user;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.panzh.UtilEntity.ResultVO;
import com.panzh.entity.index.UserInfo;
import com.panzh.myconst.CommonConst;
import com.panzh.myconst.UserContst;
import com.panzh.util.ImageUtil;
import com.wutao.panzh.service.index.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * user packge is index user admin;
 * time：2020/2/7
 * author：wutao
 */
@Controller
public class UserIndexHandler {

    @Autowired
    private ObjectMapper mapper;

    @Autowired
    private HttpServletRequest request;

    @Reference
    private UserService userService;


    @ResponseBody
    @PostMapping("/user/admin/upload/head/image")
    public ResultVO uploadHeadImage(@RequestParam("file") MultipartFile file,@RequestParam("id") String userId) throws Exception {
        try {
            byte[] bytes = file.getBytes();
            //上传图片，并进行图片的压缩
            Map<String, Object> map = ImageUtil.uploadImag(bytes, 390, 328, true);
            //获得图片的地址，进行返回；
            String imagUrl = (String) map.get("fileName");
            //这里代码需要重构。没有写完；
            userService.updateHeadImag(imagUrl,userId);
//            进行插入数据库；
            return new ResultVO(ResultVO.SUCCESS, CommonConst.INFORMATION_UPDATED_SUCCESSFULLY,imagUrl);
        } catch (Exception e) {
            return new ResultVO(ResultVO.FAILED, CommonConst.DATA_SAVING_FAILED,ResultVO.NO_DATA);
        }
    }

//    signOut
    @RequestMapping("/user/admin/signout")
    public String userSignOut(){
        //clearSession();
        clearSession();
        return "index";
    }

    @RequestMapping("/user/admin/index")
    public String getUserIndex() throws JsonProcessingException {
        getJsonUserInfo();
//        我们可以把这些代码放到过滤器里面做
//        HttpSession session = request.getSession();
//        UserInfo userInfo = (UserInfo) session.getAttribute(UserContst.USER_INFO_IS_EXIST);
//        request.setAttribute("userInfo",userInfo);
//        UserInfo info = (UserInfo) request.getAttribute("userInfo");
//        System.out.println(info);
        return "userIndex";
    }

//     <dd><a href="user/center.html">中心面板</a></dd>
    @RequestMapping("/user/admin/center")
    public String getUserCenter() throws JsonProcessingException {
        getJsonUserInfo();
        return "userCenter";
    }
//     <dd><a href="user/fundamental/set.html">基本资料</a></dd>
    @RequestMapping("/user/admin/fundamental")
    public String getUserFundamental() throws JsonProcessingException {
        getJsonUserInfo();
        return "userFundamentalSet";
    }

    //获得userInfo的json对象；
    private void getJsonUserInfo() throws JsonProcessingException {
//        System.out.println(UserIndexHandler.class.getName()+"类前置代码执行！");
        UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
        //防止密码泄露；
        userInfo.setPasswd("");
        String userInfoJson = mapper.writeValueAsString(userInfo);
//        System.out.println(userInfoJson);
        request.setAttribute(UserContst.USER_INFO,userInfoJson);
    }

    private void clearSession() {
        HttpSession session = request.getSession();
        session.setAttribute(UserContst.USER_INFO,null);
    }
}
